package me.zhengjie.modules.loan.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.loan.domain.Loan;
import me.zhengjie.modules.loan.service.LoanService;
import me.zhengjie.modules.loan.service.dto.LoanDto;
import me.zhengjie.modules.loan.service.dto.LoanQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.util.Set;

/**
* @author lida
* @date 2022-04-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "借贷管理管理")
@RequestMapping("/api/loan")
public class LoanController {

    private final LoanService loanService;

    @GetMapping
    @Log("查询借贷管理")
    @ApiOperation("查询借贷管理")
    @PreAuthorize("@el.check('loan:list')")
    public ResponseEntity query(LoanQueryParam query, Pageable pageable){
        return new ResponseEntity<>(loanService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增借贷管理")
    @ApiOperation("新增借贷管理")
    @PreAuthorize("@el.check('loan:add')")
    public ResponseEntity create(@Validated @RequestBody LoanDto resources){
        return new ResponseEntity<>(loanService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改借贷管理")
    @ApiOperation("修改借贷管理")
    @PreAuthorize("@el.check('loan:edit')")
    public ResponseEntity update(@Validated @RequestBody LoanDto resources){
        loanService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除借贷管理")
    @ApiOperation("删除借贷管理")
    @PreAuthorize("@el.check('loan:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        loanService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出借贷管理")
    @ApiOperation("导出借贷管理")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('loan:list')")
    public void download(HttpServletResponse response, LoanQueryParam query) throws IOException {
        loanService.download(loanService.queryAll(query), response);
    }*/

}
